{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b6f18e46-8b7c-487a-b8ed-a562c375c522",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6773b5d7-f34b-4362-9d0f-ec27f1b3f7a0",
   "metadata": {},
   "source": [
    "# 浮点错误处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "923bb3eb-e318-44f3-8895-6de52cdf601a",
   "metadata": {
    "toc-hr-collapsed": true
   },
   "source": [
    "## 设置和获取错误处理\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "seterr([all, divide, over, under, invalid])|设置如何处理浮点错误。\n",
    "geterr()|获取当前处理浮点错误的方法。\n",
    "seterrcall(func)|设置浮点错误回调函数或日志对象。\n",
    "geterrcall()|返回用于浮点错误的当前回调函数。\n",
    "errstate(**kwargs)|用于浮点错误处理的上下文管理器。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "90b2e69d-6bc5-4454-b0f7-bb8c37eaabfc",
   "metadata": {},
   "source": [
    "### numpy.seterr(all=None, divide=None, over=None, under=None, invalid=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e44f21f5-d9a7-41e5-8fb6-cea517504cef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30464"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orig_settings = np.seterr(all='ignore')  # seterr to known value\n",
    "np.int16(32000) * np.int16(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a336d46b-c91b-45d4-bea4-60eb9c151bbe",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.seterr(over='raise')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "3ae9978b-532c-4875-ab9a-4f19f4f104a8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "old_settings = np.seterr(all='print')\n",
    "np.geterr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d82b3875-9b54-4536-a489-6e4b193e2e0c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'print', 'over': 'print', 'under': 'print', 'invalid': 'print'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.seterr(**orig_settings)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e55842a1-8e1a-4dfb-b1b2-08b37ee0a324",
   "metadata": {},
   "source": [
    "### numpy.geterr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d4d53c03-c512-4be0-a931-1b6aabc6a0ca",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.geterr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0f7a6c49-390b-42f2-9032-cde8774512a3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'warn', 'over': 'warn', 'under': 'warn', 'invalid': 'raise'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oldsettings = np.seterr(all='warn', invalid='raise')\n",
    "np.geterr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1f9a16ee-dc03-4b1c-a23e-039421722858",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "oldsettings = np.seterr(**oldsettings)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6575b309-7fb9-4d66-bf95-79acb3b65f4b",
   "metadata": {},
   "source": [
    "### numpy.seterrcall(func)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3eaee397-4d99-4772-b8b3-f493128d4e27",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def err_handler(type, flag):\n",
    "    print(\"Floating point error (%s), with flag %s\" % (type, flag))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4628a400-f4c8-483b-bb12-fdc126e7dd08",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Floating point error (divide by zero), with flag 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([inf, inf, inf])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orig_handler = np.seterrcall(err_handler)\n",
    "orig_err = np.seterr(all='call')\n",
    "np.array([1, 2, 3]) / 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "35735bac-f551-48a1-a555-cc8344f3638f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<function __main__.err_handler(type, flag)>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.seterrcall(orig_handler)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "3d075579-f7ed-4381-ae3e-c67c3ca06d60",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'call', 'over': 'call', 'under': 'call', 'invalid': 'call'}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.seterr(**orig_err)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "265c2ba5-1da7-47ce-8ad9-a7527bb3b5ae",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "class Log:\n",
    "    def write(self, msg):\n",
    "        print(\"LOG: %s\" % msg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1b48f878-54f8-41d8-a6e3-78f25d407536",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "log = Log()\n",
    "saved_handler = np.seterrcall(log)\n",
    "save_err = np.seterr(all='log')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7dbf0d9d-258e-4d82-ab86-faca402a8554",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "LOG: Warning: divide by zero encountered in true_divide\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([inf, inf, inf])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array([1, 2, 3]) / 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9ebef7f7-159d-46c3-bd0d-e1017e0c4d5a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<__main__.Log at 0x7f9ec2ae4b38>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.seterrcall(orig_handler)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f74e8f66-88a7-450d-92b5-48e7652d6dec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'log', 'over': 'log', 'under': 'log', 'invalid': 'log'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.seterr(**orig_err)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0db69239-ed7a-401c-9758-ae564e7b4561",
   "metadata": {},
   "source": [
    "### numpy.geterrcall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bc7de684-b392-49eb-89d4-0fe1e90aaa19",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "np.geterrcall()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4946c934-d86a-4eae-bbbd-24bc016503bd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Floating point error (divide by zero), with flag 1\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([inf, inf, inf])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "orig_settings = np.seterr(all='call')\n",
    "def err_handler(type, flag):\n",
    "    print(\"Floating point error (%s), with flag %s\" % (type, flag))\n",
    "old_handler = np.seterrcall(err_handler)\n",
    "np.array([1, 2, 3]) / 0.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "3ed1ad20-5ef5-4932-9000-722d179c6748",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cur_handler = np.geterrcall()\n",
    "cur_handler is err_handler"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f63b8d43-290b-466e-8c65-bc35c646cdd9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "old_settings = np.seterr(**orig_settings)\n",
    "old_handler = np.seterrcall(None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de7e21c7-f7bd-4deb-a5d1-3253cc825fd6",
   "metadata": {},
   "source": [
    "### class numpy.errstate(**kwargs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "63b828fb-2c07-4c40-bd93-c450dbc42121",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "olderr = np.seterr(all='ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "93c41e51-664f-4feb-8a5b-c83b7968d13e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([nan, inf, inf])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.arange(3) / 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b03825b4-d52c-4c0d-b3bc-13fa58b1fa80",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "with np.errstate(divide='ignore'):\n",
    "    np.arange(3) / 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b089ce33-eedf-4dce-8eb8-cd1983dd31ff",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "nan"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sqrt(-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "20f2b033-0252-44c4-bb99-be5cd8944d8b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'divide': 'ignore', 'over': 'ignore', 'under': 'ignore', 'invalid': 'ignore'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.geterr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7bea097d-19fb-4333-8cf4-93b0bd69c8b5",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "olderr = np.seterr(**olderr)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32c7e562-626b-461b-8199-7ef35a307736",
   "metadata": {},
   "source": [
    "## 内部功能\n",
    "\n",
    "方法|描述\n",
    "--:|:--\n",
    "seterrobj(errobj)|设置定义浮点错误处理的对象。\n",
    "geterrobj()|返回定义浮点错误处理的当前对象。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
